package com.googlecode.maratische.google.testBrowser;

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.html.HTMLDocument;

/**
 * пример, созданный в попытке сделать плавный просмотр странички в JEditorPane
 * @author gizatullinm
 *
 */
public class Example1 extends JFrame {

	public static void main(String[] args) {
		Example1 example1 = new Example1();
		example1.setVisible(true);
	}
	
	private JEditorPane textPane;
	private JScrollPane jScrollPane;
	
	public Example1() {
		setSize(700, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		
		jScrollPane = new JScrollPane();
		jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		textPane = new JEditorPane();
       	textPane.setContentType( "text/html" );
        textPane.setEditable(false);
        textPane.setFocusable(false);
        jScrollPane.setFocusable(true);
//		HTMLDocument document = new HTMLDocument();
        //		textPane.setStyledDocument(document);
		jScrollPane.setViewportView(textPane);
		add(jScrollPane,BorderLayout.CENTER);

		try {
			String image1 = new File("src\\main\\resources\\image1.jpg").toURI().toString();
			String image2 = new File("src\\main\\resources\\image2.jpg").toURI().toString();
			
			BufferedReader br = new BufferedReader(new FileReader(new File("src\\main\\resources\\index.html")));
			StringBuilder sb = new StringBuilder();
			String temp = null;
			while ( (temp = br.readLine()) != null) {
				sb.append(temp);
			}
			String res = sb.toString();
			res = res.replaceAll("\\{image1\\}", image1);
			res = res.replaceAll("\\{image2\\}", image2);
			
			textPane.setText(res);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
